//
//  HMNewFeatureViewController.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/5.
//  Copyright © 2017年 Mac. All rights reserved.
//

//有4张滚动图片
#define HMNewfeatureImageCount 4


#import "HMNewFeatureViewController.h"
#import "HMTabBarViewController.h"
#import "UIImage+Extension.h"

@interface HMNewFeatureViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIPageControl *pageControl;

@end

@implementation HMNewFeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.view.backgroundColor = [UIColor yellowColor];
    //1/添加scrollView
    [self setupScrollView];
    
    //2/添加pageControl
    [self setupPageControl];

}

//1/添加scrollView
- (void)setupScrollView{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    //添加图片
    CGFloat imageW = scrollView.frame.size.width;
    CGFloat imageH = scrollView.frame.size.height;
    
    for (int i = 0; i < HMNewfeatureImageCount; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * imageW, 0, imageW, imageH)];
        NSString *imageName = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        
        /*
         3.5inch retina(960 * 640 像素)  自动加载 @2x.png
         4.0inch retina(1136 * 640 像素)  自动加载 -568h@2x.png(只适用于启动图 launchImagel里面),其他地方,依然加载@2x.png

         在Assets.xcassets 中如果图片 是Unassigned 类型的,说明Xcode不清楚该图片在什么地方用,所以该图片不会打包到bundle 中,所以在代码获取的时候取到的是空,选中该图片右键 选择iPhone, 将该图片拖到合适的位置,该操作是告诉Xcode该图片是有用的
         */
//        if (FourInch) {
//            //如果是4英寸
//            imageName = [imageName stringByAppendingString:@"-568h"];
//        }
        
        imageView.image = [UIImage imageWithName:imageName];
        
        [scrollView addSubview:imageView];
        
        scrollView.contentSize = CGSizeMake(imageW * HMNewfeatureImageCount, 0);
        scrollView.pagingEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.backgroundColor = HMColor(246, 246, 246);
        
        //处理最后一个imageview的时候
        if (i == HMNewfeatureImageCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
}

//处理最后一个imageview的内容
- (void)setupLastImageView:(UIImageView *)imageView{
    //注意打开 UIImageView 的交互
    imageView.userInteractionEnabled = YES;
    
    //添加 开始 按钮
    [self setupStartButton:imageView];
    
    //添加 分享 按钮
    [self setupShareButton:imageView];
    
    
}

#pragma mark ======== //添加 开始 按钮 ========
- (void)setupStartButton:(UIImageView *)imageView{
    //1/添加 按钮
    UIButton *startButton = [[UIButton alloc] init];
    [imageView addSubview:startButton];
    //2/设置背景
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    //3/设置frame
    startButton.size = startButton.currentBackgroundImage.size;
    startButton.centerX = self.view.width * 0.5;
    startButton.centerY = self.view.height * 0.8;
    //4/设置文字
    [startButton setTitle:@"开始微博" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //5/添加 方法
    [startButton addTarget:self action:@selector(startButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)startButtonClick{
    //显示状态栏
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[HMTabBarViewController alloc] init];
    
}

#pragma mark =========== //添加 分享 按钮 ============
- (void)setupShareButton:(UIImageView *)imageView{
    //1.添加按钮
    UIButton *shareButton = [[UIButton alloc] init];
    [imageView addSubview:shareButton];
    //2.设置文字和图标
    [shareButton setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];
    
    //3.设置frame
    shareButton.size = CGSizeMake(150, 35);
    shareButton.centerX = self.view.width * 0.5;
    shareButton.centerY = self.view.height * 0.7;
    shareButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);

    //4.添加方法
    [shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

//点击 按钮,切换图片(打钩和无沟)
- (void)shareButtonClick:(UIButton *)button{
    button.selected = !button.selected;

}


//2/添加pageControl
- (void)setupPageControl{
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = HMNewfeatureImageCount;
    pageControl.centerX = self.view.frame.size.width * 0.5;
    pageControl.centerY = self.view.frame.size.height - 30;
    [self.view addSubview:pageControl];
    
    //非当前页圆点颜色
    pageControl.pageIndicatorTintColor = HMColor(189, 189, 189);
    //当前页的圆点颜色
    pageControl.currentPageIndicatorTintColor = HMColor(253, 98, 42);
    
    self.pageControl = pageControl;

}

#pragma mark = UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //细节:左右滑动超过一半,下面的圆点才变化
    CGFloat doublePage = scrollView.contentOffset.x / scrollView.frame.size.width;
    //对page 进行四舍五入,强转为整型,即把小数部分直接抹掉
    int page = (int)(doublePage + 0.5);
    
    self.pageControl.currentPage = page;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
